var user_id;
var current_page = 1;

$(function() {
    createOrderLineColor();
    $('.user_search_by_date_in').hide();
    $('.user_search_by_date_out').hide();

    user_id = $('.order_id_search').attr('value');

    $('.user_search_by select').change(function() {
        if (parseInt($(this).val()) === 2) {
            $('.user_search_by_date_in').show();
            $('.user_search_by_date_out').show();
        } else {
            $('.user_search_by_date_in').hide();
            $('.user_search_by_date_out').hide();
        }
    });

    $('#user_search_by_date_in_input').datepicker({
        dateFormat: 'dd/mm/yy',
        onClose: function(selectedDate) {
            $("#user_search_by_date_out_input").datepicker("option", "minDate", selectedDate);
        }
    });

    $('.user_search_by_date_in img').click(function() {
        $('#user_search_by_date_in_input').datepicker("show");
    });

    $('#user_search_by_date_out_input').datepicker({
        dateFormat: 'dd/mm/yy'
    });

    $('.user_search_by_date_out img').click(function() {
        $('#user_search_by_date_out_input').datepicker("show");
    });

//    getOrders(current_page, 0, 0, 0);
});

function getOrders(page, order_id, from, to) {
    var ordersRes = {
        id: $('.order_id_search').attr('value'),
        order_id: order_id,
        from_date: from,
        to_date: to,
        page: page
    };

    $.ajax({
        type: 'POST',
        url: url + 'orders',
        data: ordersRes,
        success: function(msg) {
            var json = $.parseJSON(msg);
            $(json).each(function(i, v) {
                insertOrders(v.orders);
                insertPage(v.row);
            });
        }
    });
}

function insertOrders(orders) {
    $.each(orders, function(i, v) {
//                            <div class="order_line">
//                        <div class="order_line_id">id</div>
//                        <div class="order_line_id_from_date">from date</div>
//                        <div class="order_line_id_to_date">to date</div>
//                        <div class="order_line_id_city">city</div>
//                        <div class="order_line_id_hotel">hotel</div>
//                        <div class="order_line_id_amount">amount</div>
//                        <div class="order_line_id_commission">commission</div>
//                        <div class="order_line_id_view_button">view</div>
//                    </div>
    });
}

function insertPage(page) {

} 

function getCurrentDate() {
    var d = new Date();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    var output = d.getFullYear() + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            (day < 10 ? '0' : '') + day;

    return output;
}

function createOrderLineColor() {
    $('.orders_container .order_line').each(function(i, v) {
        if (i % 2 === 0) {
            $(this).css({
                'background-color': '#ffffff'
            });
        } else {
            $(this).css({
                'background-color': '#ededed'
            });
        }
    });
}


